﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Web.Old_App_Code;

public partial class backstage_key_words : System.Web.UI.Page
{
    // Fields

    protected KeyWords kw = new KeyWords();
    protected KeyWordsManager kwm = new KeyWordsManager();

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.kw.KeyWord = this.txtKeyWord.Value;
            this.kw.ClickCount = Convert.ToInt32(this.txtClickCount.Value);
            if (this.rdIsShow0.Checked)
            {
                this.kw.IsShow = 0;
            }
            else
            {
                this.kw.IsShow = 1;
            }
            this.kw.LanguageVersion = this.Session["language_version"].ToString();
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.kw.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                if (this.kwm.ModifyKeyWords(this.kw))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"key_words_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
            else if (this.kwm.AdminAddKeyWords(this.kw))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"key_words_list.aspx\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (base.Request.QueryString["ot"] == null)
            {
                return builder;
            }
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                builder.Append("编辑关键词");
                return builder;
            }
            builder.Append("添加关键词");
        }
        catch
        {
        }
        return builder;
    }

    private void KeyWordsBind()
    {
        try
        {
            DataTable keyWordsById = this.kwm.GetKeyWordsById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (keyWordsById.Rows.Count > 0)
            {
                this.txtKeyWord.Value = keyWordsById.Rows[0]["keyWord"].ToString();
                this.txtClickCount.Value = keyWordsById.Rows[0]["clickCount"].ToString();
                if (Convert.ToInt32(keyWordsById.Rows[0]["isShow"]) == 1)
                {
                    this.rdIsShow1.Checked = true;
                }
                else
                {
                    this.rdIsShow0.Checked = true;
                }
            }
        }
        catch
        {
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.KeyWordsBind();
            }
            else
            {
                this.rdIsShow1.Checked = true;
            }
        }
    }

}